package cn.edu.ncut.cs.springboot.springbootmybatisplus.controller;

import cn.edu.ncut.cs.springboot.springbootmybatisplus.entity.Course;
import cn.edu.ncut.cs.springboot.springbootmybatisplus.entity.Student;
import cn.edu.ncut.cs.springboot.springbootmybatisplus.entity.Teacher;
import cn.edu.ncut.cs.springboot.springbootmybatisplus.service.ICourseService;
import cn.edu.ncut.cs.springboot.springbootmybatisplus.service.IStudentService;
import cn.edu.ncut.cs.springboot.springbootmybatisplus.service.ITeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ncut
 * @since 2024-12-07
 */
@Controller
@RequestMapping("teacher")
public class TeacherController {
    @Autowired
    private ITeacherService teacherService;
    @Autowired
    private ICourseService courseService;
    @RequestMapping("listTeacher")
    public String listStudent(Model model, @RequestParam(required = false) String tname) {
        QueryWrapper<Teacher> qw=new QueryWrapper<>();
        if (tname != null && !tname.isEmpty()) {
            qw.like("tname", tname);
        }
        List<Course> courseList=courseService.list();
        model.addAttribute("courseList",courseList);
        List<Teacher> list=teacherService.list(qw);
        model.addAttribute("teachers",list);
        return "admin-teacher-list";
    }
    @RequestMapping("saveTeacher")
    public String saveTeacher(Teacher teacher){
        teacherService.save(teacher);
        return "redirect:/teacher/listTeacher";
    }
    @RequestMapping("preUpdateTeacher/{tno}")
    public String preUpdateTeacher(@PathVariable String tno, Model model){
        Teacher teacher=teacherService.getById(tno);
        model.addAttribute("teacher",teacher);
        List<Course> courseList=courseService.list();
        model.addAttribute("courseList",courseList);
        return "admin-teacher-update";
    }
    @RequestMapping("updateTeacher")
    public String updateTeacher(Teacher teacher){
        boolean b=teacherService.updateById(teacher);
        return "redirect:/teacher/listTeacher";
    }
    @RequestMapping("delTeacher/{tno}")
    public String delTeacher(@PathVariable Integer tno){
        boolean b=teacherService.removeById(tno);
        return "redirect:/teacher/listTeacher";
    }
}
